package org.setaria.cover
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	import mx.managers.PopUpManagerChildList;
	
	import spark.components.Application;

	public class CoverManager extends UIComponent
	{
		private static var _instance:CoverManager = null;
		
		private var _coverComponent:UIComponent = FlexGlobals.topLevelApplication as UIComponent;;
		
		private var _coverWindow:BaseCaver = new CoverWindow();
		
		public function CoverManager()
		{
			if(!_instance){
				super();
			}else
				throw new Error("Error: Singletons can only be instantiated via instance() method!");
		}
		
		public static function instance():CoverManager{
			if(!_instance){
				_instance = new CoverManager();
			}
			return _instance;
		}
		
		public function get coverComponent() : UIComponent
		{
			return this._coverComponent;
		}
		
		public function set coverComponent(coverComponent:UIComponent) : void
		{
			this._coverComponent = coverComponent;
			return;
		}
		public function get coverWindow() : BaseCaver
		{
			return this._coverWindow;
		}
		
		public function set coverWindow(coverWindow:BaseCaver) : void
		{
			this._coverWindow = coverWindow;
			return;
		}
		
		/**
		 *使用此方法控制遮罩层的显示
		 * @param flag true 显示；false 取消
		 * 
		 * */
		public function cover(flag:Boolean):void{
			if (flag)
			{
				PopUpManager.addPopUp(coverWindow, (FlexGlobals.topLevelApplication as DisplayObject), false, PopUpManagerChildList.APPLICATION);
				coverWindow.width = coverComponent.width;
				coverWindow.height = coverComponent.height;
				var point:Point = coverComponent.parent.localToGlobal(new Point(coverComponent.x, coverComponent.y));
				coverWindow.x = point.x; 
				coverWindow.y = point.y;
			}
			else
			{
				if (!coverWindow.isPopUp) return;
				PopUpManager.removePopUp(coverWindow);
				coverWindow.width = coverWindow.height = 0;
			}
		}
		
		public function set loadInfo(info:String):void{
			coverWindow.tooltip = info;
		}
	}
}